<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="证书编号" prop="certificateNumber">
        <el-input
          v-model="queryParams.certificateNumber"
          placeholder="请输入证书编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书名称" prop="certificateName">
        <el-input
          v-model="queryParams.certificateName"
          placeholder="请输入证书名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书性质" prop="certificateNature">
        <el-select
          v-model="queryParams.certificateNature"
          placeholder="请选择证书性质"
          clearable
        >
          <el-option
            v-for="dict in dict.type.zhjj_certificate_nature"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单位名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位编码" prop="companyCode">
        <el-input
          v-model="queryParams.companyCode"
          placeholder="请输入单位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="持有人" prop="holder">
        <el-input
          v-model="queryParams.holder"
          placeholder="请输入持有人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="持有人ID" prop="holderIdNumber">
        <el-input
          v-model="queryParams.holderIdNumber"
          placeholder="请输入持有人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书类型" prop="certificateType">
        <el-input
          v-model="queryParams.certificateType"
          placeholder="请输入证书类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发证日期">
        <el-date-picker
          v-model="daterangeIssueDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="有效截止日期">
        <el-date-picker
          v-model="daterangeValidityPeriodEnd"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="发证机关" prop="issuingAuthority">
        <el-input
          v-model="queryParams.issuingAuthority"
          placeholder="请输入发证机关"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证结果" prop="certificationResult">
        <el-input
          v-model="queryParams.certificationResult"
          placeholder="请输入认证结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书状态" prop="certificationStatus">
        <el-select
          v-model="queryParams.certificationStatus"
          placeholder="请选择证书状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.zhjj_certificate_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zhjj:qualification:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zhjj:qualification:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zhjj:qualification:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zhjj:qualification:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="qualificationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据发送方ID" align="center" prop="extSysId" />
      <el-table-column
        label="证书编号"
        align="center"
        prop="certificateNumber"
      />
      <el-table-column label="证书名称" align="center" prop="certificateName" />
      <el-table-column label="证书性质" align="center" prop="certificateNature">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.zhjj_certificate_nature"
            :value="scope.row.certificateNature"
          />
        </template>
      </el-table-column>
      <el-table-column label="单位名称" align="center" prop="companyName" />
      <el-table-column label="单位编码" align="center" prop="companyCode" />
      <el-table-column label="持有人" align="center" prop="holder" />
      <el-table-column label="持有人ID" align="center" prop="holderIdNumber" />
      <el-table-column label="证书类型" align="center" prop="certificateType" />
      <el-table-column
        label="发证日期"
        align="center"
        prop="issueDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.issueDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="有效截止日期"
        align="center"
        prop="validityPeriodEnd"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.validityPeriodEnd, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="发证机关"
        align="center"
        prop="issuingAuthority"
      />
      <el-table-column
        label="认证结果"
        align="center"
        prop="certificationResult"
      />
      <el-table-column
        label="证书状态"
        align="center"
        prop="certificationStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.zhjj_certificate_status"
            :value="scope.row.certificationStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="证书文件URL"
        align="center"
        prop="certificateFileUrl"
      />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zhjj:qualification:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zhjj:qualification:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改证书信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        inline
      >
        <el-form-item label="证书编号" prop="certificateNumber">
          <el-input
            v-model="form.certificateNumber"
            placeholder="请输入证书编号"
          />
        </el-form-item>
        <el-form-item label="证书名称" prop="certificateName">
          <el-input
            v-model="form.certificateName"
            placeholder="请输入证书名称"
          />
        </el-form-item>
        <el-form-item label="证书性质" prop="certificateNature">
          <el-select
            v-model="form.certificateNature"
            placeholder="请选择证书性质"
          >
            <el-option
              v-for="dict in dict.type.zhjj_certificate_nature"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位名称" prop="companyName">
          <el-input
            v-model="form.companyName"
            placeholder="请输入单位名称"
            style="width: 130px"
            :disabled="true"
          />
          <el-button @click="openDialog">选择</el-button>
        </el-form-item>
        <el-form-item label="单位编码" prop="companyCode">
          <el-input
            v-model="form.companyCode"
            placeholder="请输入单位编码"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="持有人" prop="holder">
          <el-input v-model="form.holder" placeholder="请输入持有人" />
        </el-form-item>
        <el-form-item label="持有人ID" prop="holderIdNumber">
          <el-input
            v-model="form.holderIdNumber"
            placeholder="请输入持有人ID"
          />
        </el-form-item>
        <el-form-item label="证书类型" prop="certificateType">
          <el-input
            v-model="form.certificateType"
            placeholder="请输入证书类型"
          />
        </el-form-item>
        <el-form-item label="发证日期" prop="issueDate">
          <el-date-picker
            clearable
            v-model="form.issueDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发证日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="有效截止日期"
          prop="validityPeriodEnd"
          label-width="120px"
        >
          <el-date-picker
            clearable
            v-model="form.validityPeriodEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效截止日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发证机关" prop="issuingAuthority">
          <el-input
            v-model="form.issuingAuthority"
            placeholder="请输入发证机关"
          />
        </el-form-item>
        <el-form-item label="认证结果" prop="certificationResult">
          <el-input
            v-model="form.certificationResult"
            placeholder="请输入认证结果"
          />
        </el-form-item>
        <el-form-item label="证书状态" prop="certificationStatus">
          <el-radio-group v-model="form.certificationStatus">
            <el-radio
              v-for="dict in dict.type.zhjj_certificate_status"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item
          label="证书文件URL"
          prop="certificateFileUrl"
          label-width="100px"
        >
          <el-input
            v-model="form.certificateFileUrl"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <ContractorDialog
      :visible.sync="dialogVisible"
      @selectData="handleSelectData"
    />
  </div>
</template>

<script>
import ContractorDialog from "@/components/ContractorDialog/index.vue";
import {
  listQualification,
  getQualification,
  delQualification,
  addQualification,
  updateQualification,
} from "@/api/zhjj/qualification";

export default {
  name: "Qualification",
  dicts: ["zhjj_certificate_nature", "zhjj_certificate_status"],
  components: {
    ContractorDialog,
  },
  data() {
    return {
      // 承包商弹出层
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 证书信息表格数据
      qualificationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 证书文件URL时间范围
      daterangeIssueDate: [],
      // 证书文件URL时间范围
      daterangeValidityPeriodEnd: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        certificateNumber: null,
        certificateName: null,
        certificateNature: null,
        companyName: null,
        companyCode: null,
        holder: null,
        holderIdNumber: null,
        certificateType: null,
        issueDate: null,
        validityPeriodEnd: null,
        issuingAuthority: null,
        certificationResult: null,
        certificationStatus: null,
        certificateFileUrl: null,
        createBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // 证书编号
        certificateNumber: [
          { required: true, message: "证书编号不能为空", trigger: "blur" },
          { max: 30, message: "证书编号长度不能超过30个字符", trigger: "blur" },
        ],

        // 证书名称
        certificateName: [
          { required: true, message: "证书名称不能为空", trigger: "blur" },
          {
            max: 100,
            message: "证书名称长度不能超过100个字符",
            trigger: "blur",
          },
        ],

        // 单位名称
        companyName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" },
          {
            max: 100,
            message: "单位名称长度不能超过100个字符",
            trigger: "blur",
          },
        ],

        // 单位编码
        companyCode: [
          { required: true, message: "单位编码不能为空", trigger: "blur" },
          { max: 30, message: "单位编码长度不能超过30个字符", trigger: "blur" },
        ],

        // 持有人
        holder: [
          { max: 60, message: "持有人长度不能超过60个字符", trigger: "blur" },
        ],

        // 持有人ID(工号)
        holderIdNumber: [
          { max: 30, message: "持有人ID长度不能超过30个字符", trigger: "blur" },
        ],

        // 证书类型
        certificateType: [
          { max: 10, message: "证书类型长度不能超过10个字符", trigger: "blur" },
        ],

        // 发证日期
        issueDate: [
          { type: "string", message: "请选择有效的发证日期", trigger: "blur" },
        ],

        // 有效截止日期
        validityPeriodEnd: [
          { required: true, message: "有效截止日期不能为空", trigger: "blur" },
          {
            type: "string",
            message: "请选择有效的有效截止日期",
            trigger: "blur",
          },
        ],

        // 发证机关
        issuingAuthority: [
          {
            max: 100,
            message: "发证机关长度不能超过100个字符",
            trigger: "blur",
          },
        ],

        // 认证结果
        certificationResult: [
          {
            max: 100,
            message: "认证结果长度不能超过100个字符",
            trigger: "blur",
          },
        ],

        // 证书状态
        certificationStatus: [
          { max: 10, message: "证书状态长度不能超过10个字符", trigger: "blur" },
        ],

        // 证书文件URL
        certificateFileUrl: [
          {
            max: 500,
            message: "证书文件URL长度不能超过500个字符",
            trigger: "blur",
          },
        ],

        // 创建人
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" },
        ],

        // 创建时间
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询证书信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeIssueDate && "" != this.daterangeIssueDate) {
        this.queryParams.params["beginIssueDate"] = this.daterangeIssueDate[0];
        this.queryParams.params["endIssueDate"] = this.daterangeIssueDate[1];
      }
      if (
        null != this.daterangeValidityPeriodEnd &&
        "" != this.daterangeValidityPeriodEnd
      ) {
        this.queryParams.params["beginValidityPeriodEnd"] =
          this.daterangeValidityPeriodEnd[0];
        this.queryParams.params["endValidityPeriodEnd"] =
          this.daterangeValidityPeriodEnd[1];
      }
      listQualification(this.queryParams).then((response) => {
        this.qualificationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        extSysId: null,
        certificateNumber: null,
        certificateName: null,
        certificateNature: null,
        companyName: null,
        companyCode: null,
        holder: null,
        holderIdNumber: null,
        certificateType: null,
        issueDate: null,
        validityPeriodEnd: null,
        issuingAuthority: null,
        certificationResult: null,
        certificationStatus: null,
        remark: null,
        certificateFileUrl: null,
        createBy: null,
        delFlag: null,
        updateBy: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeIssueDate = [];
      this.daterangeValidityPeriodEnd = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.extSysId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加证书信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const extSysId = row.extSysId || this.ids;
      getQualification(extSysId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改证书信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.extSysId != null) {
            updateQualification(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQualification(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const extSysIds = row.extSysId || this.ids;
      this.$modal
        .confirm('是否确认删除证书信息编号为"' + extSysIds + '"的数据项？')
        .then(function () {
          return delQualification(extSysIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "zhjj/qualification/export",
        {
          ...this.queryParams,
        },
        `qualification_${new Date().getTime()}.xlsx`
      );
    },
    openDialog() {
      this.dialogVisible = true;
    },

    // 选中数据并关闭对话框
    handleSelectData(row) {
      this.form.companyName = row.companyName;
      this.form.companyCode = row.companyCode;
    },
  },
};
</script>
